package com.zego.wawaji.ui.wawa.synth.material;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.third.image.ImageLoader;
import com.zego.wawaji.ui.base.ExBaseAdapter;
import com.zego.wawaji.ui.bean.wawa.Wawa;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;
import com.zego.wawaji.ui.dialog.SimpleTipDialog;
import com.zego.wawaji.ui.view.counter.SnappingStepper;
import com.zego.wawaji.wawaji_client.AppTinkerLike;

import java.util.List;

/**
 * Created by Su on 2018/3/12.
 */
public class MaterialAdapter extends ExBaseAdapter<Wawa> {
    public MaterialAdapter(Context context, List<Wawa> datas) {
        super(context, datas);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyViewHolder holder;
        if (convertView == null) {
            convertView = inflate(R.layout.material_item);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }

        final Wawa wawa = getItem(position);
        ImageLoader.loadCenterCrop(getContext(), wawa.getImg(), holder.material_icon);
        holder.material_name.setText(wawa.getName());
        holder.material_pro.setText(String.valueOf(wawa.getPr()));
        holder.material_remark.setText(wawa.getRemark());
        if (wawa.getAmountWwCoin() > 0) {
            holder.material_lie.setText("原价" + wawa.getAmountWwCoin() + "金币");
            holder.material_lie.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            holder.material_lie.setVisibility(View.VISIBLE);
        }
        holder.material_price.setText(String.valueOf(wawa.getExchange()));

        holder.stepper.setOnValueChangeListener((view, value) -> holder.num = value);
        holder.btn_exchange.setOnClickListener(v -> exchange(wawa, holder.num));
        return convertView;
    }

    private void exchange(final Wawa wawa, final int num) {
        String content = "确认兑换" + num + "个" + wawa.getName() + "吗？";
        PickerDialogUtil.showSimpleTipDialogExt((FragmentActivity) AppTinkerLike.getActivity(), new SimpleTipDialog.ConfirmListener() {
                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onSubmit() {
                        ModuleMgr.getWawaMgr().reqExchangeMaterial(wawa.getId(), num, null);
                    }
                }, content, "温馨提示", "取消", "确认",
                true, R.color.text_gray);
    }

    private class MyViewHolder {
        private int num = 1;
        private SnappingStepper stepper;
        private RelativeLayout btn_exchange;
        private ImageView material_icon;
        private TextView material_name, material_pro, material_remark, material_lie, material_price;

        public MyViewHolder(View convertView) {
            initView(convertView);
        }

        private void initView(View convertView) {
            btn_exchange = (RelativeLayout) convertView.findViewById(R.id.btn_exchange);
            material_icon = (ImageView) convertView.findViewById(R.id.material_icon);
            material_name = (TextView) convertView.findViewById(R.id.material_name);
            material_pro = (TextView) convertView.findViewById(R.id.material_pro);
            material_remark = (TextView) convertView.findViewById(R.id.material_remark);
            material_lie = (TextView) convertView.findViewById(R.id.material_lie);
            material_price = (TextView) convertView.findViewById(R.id.material_price);
            stepper = (SnappingStepper) convertView.findViewById(R.id.stepper);
        }
    }
}
